package com.laizhenghua.share.device.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.laizhenghua.share.common.core.exception.ServiceException;
import com.laizhenghua.share.device.domain.entity.PowerBank;
import com.laizhenghua.share.device.mapper.PowerBankMapper;
import com.laizhenghua.share.device.service.IPowerBankService;
import com.laizhenghua.share.system.api.model.LoginUser;
import com.share.common.security.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * TODO
 *
 * @Description
 * @Author laizhenghua
 * @Date 2025/6/29 11:38
 **/
@Service
public class IPowerBankServiceImpl extends ServiceImpl<PowerBankMapper, PowerBank> implements IPowerBankService {
    private final PowerBankMapper powerBankMapper;

    @Autowired
    public IPowerBankServiceImpl(PowerBankMapper powerBankMapper) {
        this.powerBankMapper = powerBankMapper;
    }

    @Override
    public List<PowerBank> searchPageList(PowerBank powerBank) {
        return powerBankMapper.searchPageList(powerBank);
    }

    @Override
    public PowerBank customSave(PowerBank powerBank) {
        String powerBankNo = powerBank.getPowerBankNo(); // 充电宝编号
        if (ObjectUtil.isNull(powerBank.getId())) {
            LambdaQueryWrapper<PowerBank> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(PowerBank::getPowerBankNo, powerBankNo);
            Long count = powerBankMapper.selectCount(wrapper);
            if (ObjectUtil.isNotNull(count) && count > 0) {
                throw new ServiceException("充电宝编号已存在!");
            }
        }
        LoginUser user = SecurityUtils.getLoginUser();
        if (ObjectUtil.isNotNull(powerBank.getCreateTime())) {
            Long id = powerBank.getId();
            PowerBank oldPowerbank = this.getById(id);
            String status = oldPowerbank.getStatus();
            if (!"0".equals(status)) {
                throw new ServiceException("充电宝已投放使用,无法变更!");
            }
            powerBank.setCreateTime(new Date());
            if (ObjectUtil.isNotNull(user)) {
                powerBank.setCreateBy(user.getUsername());
            }
        }
        if (ObjectUtil.isNotNull(powerBank.getId())) {
            powerBank.setUpdateTime(new Date());
            if (ObjectUtil.isNotNull(user)) {
                powerBank.setUpdateBy(user.getUsername());
            }
        }
        boolean s = this.saveOrUpdate(powerBank);
        if (s) {
            return this.getById(powerBank.getId());
        }
        return null;
    }

    @Override
    public PowerBank getByPowerBankNo(String powerBankNo) {
        LambdaQueryWrapper<PowerBank> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PowerBank::getPowerBankNo, powerBankNo);
        return this.getOne(wrapper);
    }
}
